package net.micro.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.micro.dto.User;
import net.micro.dto.UserTokenMenu;
import net.micro.mapper.MicroAppMapper;
import net.micro.mapper.SystemMenuMapper;
import net.micro.model.MicroAppDO;
import net.micro.model.SystemMenuDO;
import net.micro.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private SystemMenuMapper systemMenuMapper;

    @Resource
    private MicroAppMapper microAppMapper;

    @Override
    public UserTokenMenu menuTree() {
        User user = new User();
        user.setId("1");
        user.setNickname("admin");
        user.setUsername("admin");
        UserTokenMenu userTokenMenu = new UserTokenMenu();
        userTokenMenu.setUser(user);
        userTokenMenu.setToken("123456789");
        userTokenMenu.setTicket("123456789");
        List<SystemMenuDO> systemMenuDOS = systemMenuMapper.selectList(new
                LambdaQueryWrapper<SystemMenuDO>().eq(SystemMenuDO::getDeleted, false));
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        List<MicroAppDO> microAppDOS = microAppMapper.selectList(new
                LambdaQueryWrapper<MicroAppDO>().eq(MicroAppDO::getDeleted, false));
        // 自定义属性名 都有默认值的
        treeNodeConfig.setWeightKey("sort");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setChildrenKey("children");
        // 最大递归深度
//        treeNodeConfig.setDeep(4);
        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.build(systemMenuDOS, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    MicroAppDO microAppDO = null;
                    if (treeNode.getMicroId()!=null){
                        microAppDO = microAppDOS.stream().filter(p -> p.getId().equals(treeNode.getMicroId())).findFirst().orElse(null);
                    }
                    tree.setId(String.valueOf(treeNode.getId()));
                    tree.setParentId(String.valueOf(treeNode.getParentId()));
                    tree.setWeight(treeNode.getSort());
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    tree.putExtra("url", treeNode.getUrl());
                    tree.putExtra("component", treeNode.getComponent());
                    tree.putExtra("sort", treeNode.getSort());
                    tree.putExtra("type", treeNode.getType());
                    tree.putExtra("microApp", microAppDO);
                    tree.putExtra("affix",true);
                    if(microAppDO!=null && treeNode.getUrl()!=null){
                        tree.putExtra("value",'/'+microAppDO.getAppName()+ treeNode.getUrl());
                    }else{
                        if (treeNode.getUrl()!=null){
                            tree.putExtra("value",treeNode.getUrl());
                        }else{
                            tree.putExtra("value",tree.getId());
                        }
                    }
                });

        userTokenMenu.setUserMenus(treeNodes);
        userTokenMenu.setMicroApp(microAppDOS);
        return userTokenMenu;
    }
}
